package com.lovo.project.servlet.RolePermission;

import com.lovo.project.enums.ProductEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.PermissionDO;
import com.lovo.project.service.RolePermissionService;
import com.lovo.project.service.impl.RolePermissionServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @title findOtherPermissionByRoleId 角色权限查找不是这个角色的权限url
 *        findPermissionByRoleId 角色权限查找这个角色的权限url
 * @author: chenqiang
 * @date: 2020/11/4 16:28
 */
@WebServlet(name = "RolePermissionFindThisServlet",value = "/RolePermission/rolePermissionFind")
public class RolePermissionFindServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        RolePermissionService service=new RolePermissionServiceImpl();
        int roleId=Integer.parseInt(request.getParameter("roleId"));

        Map<String, Object> map = new HashMap<>();
        PrintWriter out = response.getWriter();
        try {
            List<PermissionDO> permissionDO=service.findPermissionByRoleId(roleId);
            List<PermissionDO> otherPermissionDO=service.findOtherPermissionByRoleId(roleId);
            map.put("all",permissionDO);
            map.put("have",otherPermissionDO);
            out.print(ResultVO.success().add(map).toJson());
        } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
        }catch (Exception e1){
            out.print(ResultVO.fail(ProductEnum.PRODUCT_Update_ENUM).toJson());
        }finally {
            out.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }
}
